package com.javaee.ch11.mapper;



import com.javaee.ch11.entity.Department;
import com.javaee.ch11.web.dto.DepartmentQueryDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface DepartmentMapper {

    @Select("SELECT * FROM sys_dept")
    List<Department> selectAll();

    @Select("SELECT * FROM sys_dept WHERE id = #{id}")
    Department selectById(Long id);

    @Insert("INSERT INTO sys_dept (name, manager) VALUES (#{name}, #{manager})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Department department);

    @Update("UPDATE sys_dept SET name = #{name}, manager = #{manager} WHERE id = #{id}")
    int updateById(Department department);

    @Delete("DELETE FROM sys_dept WHERE id = #{id}")
    int deleteById(Long id);

    @Select("<script>" +
            "SELECT * FROM sys_dept " +
            "<where>" +
            "<if test='id!= null'>" +
            " AND id = #{id}" +
            "</if>" +
            "<if test='name!= null and name!= \"\"'>" +
            " AND name LIKE CONCAT('%', #{name}, '%')" +
            "</if>" +
            "<if test='manager!= null and manager!= \"\"'>" +
            " AND manager LIKE CONCAT('%', #{manager}, '%')" +
            "</if>" +
            "</where>" +
            " ORDER BY id DESC" +
            "</script>")
    List<Department> queryDepartments(DepartmentQueryDto queryDto);
}    